{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Import dependencies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import h5py\n",
    "import xarray as xr\n",
    "import glob\n",
    "import utils as ut"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Function definitions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def readATL10data(fileT, beam='gt1l'):\n",
    "    \"\"\" ATL10 reader\n",
    "    Adapted from code written by Alek Petty, June 2018 (alek.a.petty@nasa.gov)\n",
    "\n",
    "    Args:\n",
    "        fileT (str): File path of the ATL10 dataset\n",
    "        beamStr (str): ICESat-2 beam (the number is the pair, r=strong, l=weak) - default is the gt1l beam\n",
    "\n",
    "    returns:\n",
    "        numpy arrays for each quantity of interest\n",
    "\n",
    "    \"\"\"\n",
    "\n",
    "    print('ATL10 file:', fileT)\n",
    "    \n",
    "    f1 = h5py.File(fileT, 'r')\n",
    "    \n",
    "    freeboard=f1[beam]['freeboard_beam_segment']['beam_freeboard']['beam_fb_height'][:]\n",
    "\n",
    "    freeboard_confidence=f1[beam]['freeboard_beam_segment']['beam_freeboard']['beam_fb_confidence'][:]\n",
    "    freeboard_quality=f1[beam]['freeboard_beam_segment']['beam_freeboard']['beam_fb_quality_flag'][:]\n",
    "    \n",
    "    lons=f1[beam]['freeboard_beam_segment']['beam_freeboard']['longitude'][:]\n",
    "    lats=f1[beam]['freeboard_beam_segment']['beam_freeboard']['latitude'][:]\n",
    "    deltaTime=f1[beam]['freeboard_beam_segment']['beam_freeboard']['delta_time'][:]-f1[beam]['freeboard_beam_segment']['beam_freeboard']['delta_time'][0]\n",
    "    \n",
    "    # #Add this value to delta time parameters to compute full gps_seconds\n",
    "    atlas_epoch=f1['/ancillary_data/atlas_sdp_gps_epoch'][:] \n",
    "    \n",
    "    # Conversion of delta_time to a calendar date\n",
    "    temp = ut.convert_GPS_time(atlas_epoch[0] + deltaTime, OFFSET=0.0)\n",
    "    \n",
    "    \n",
    "    year = temp['year'][:].astype('int')\n",
    "    month = temp['month'][:].astype('int')\n",
    "    day = temp['day'][:].astype('int')\n",
    "    hour = temp['hour'][:].astype('int')\n",
    "    minute = temp['minute'][:].astype('int')\n",
    "    second = temp['second'][:].astype('int')\n",
    "    dFtime=pd.DataFrame({'year':year, 'month':month, 'day':day, \n",
    "                        'hour':hour, 'minute':minute, 'second':second})\n",
    "    \n",
    "    dF = pd.DataFrame({'freeboard':freeboard, 'lon':lons, 'lat':lats, 'delta_time':deltaTime,\n",
    "                      'year':year, 'month':month, 'day':day})\n",
    "    \n",
    "    dFtimepd=pd.to_datetime(dFtime)\n",
    "    \n",
    "    return lons, lats, dFtimepd, freeboard"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Test function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ATL10 file: data/ATL10_tracks/161185873/processed_ATL10-01_20181106165654_05970101_001_01.h5\n",
      "[0.05008644 0.05008644 0.05506223 ... 0.05874156 0.04574512 0.05676249]\n"
     ]
    }
   ],
   "source": [
    "file = 'data/ATL10_tracks/161185873/processed_ATL10-01_20181106165654_05970101_001_01.h5'\n",
    "lons, lats, time, freeboard = readATL10data(file)\n",
    "print(freeboard)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Find list of data files\n",
    "Get list of data files with `glob` library"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "directory = '/home/jovyan/segtrax/data/ATL10_tracks/*'\n",
    "files = glob.glob(directory)\n",
    "# print(files)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Initialize list of objects\n",
    "Iterate through each file and create a trajectory object with that file's data. Then add the object to a list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "traj_list = []\n",
    "\n",
    "for f in files:\n",
    "    \n",
    "    lons, lats, time, freeboard = readATL10data(f)\n",
    "    \n",
    "#     traj_list.append(trajectory(lons, lats, time, freeboard))\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
